package br.inf.mensageiro.view;

import java.awt.BorderLayout;
import java.awt.Font;
import java.net.InetAddress;
import java.net.MulticastSocket;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.tree.DefaultMutableTreeNode;

import br.inf.mensageiro.tree.CustomTreeCellRerender;
import br.inf.mensageiro.util.GUIUtil;
import br.inf.mensageiro.util.Imagem;
import br.inf.mensageiro.util.GUIUtil.CompCenterCords;
import br.inf.mensageiro.view.listener.ExitActionListener;
import br.inf.mensageiro.view.listener.JTreeMouseListener;


public class MainView extends JFrame{

	private static final long serialVersionUID = -4819025906370311549L;	
	

	private DefaultMutableTreeNode top;
	private JTree mainTree;
	
	public MainView(String title,MulticastSocket multicastSoc,InetAddress group){
		super(title);
		
		JDesktopPane dp=new JDesktopPane();		
		dp.setLayout(new BorderLayout());
		JMenuBar menuBar=new JMenuBar();
		top =new DefaultMutableTreeNode("root");		
		mainTree=new JTree(top);
		 //Enable tool tips.
	    ToolTipManager.sharedInstance().registerComponent(mainTree);		
		mainTree.setCellRenderer(new CustomTreeCellRerender());
		mainTree.addMouseListener(new JTreeMouseListener(this));
		JScrollPane mtsp=new JScrollPane(mainTree);
		dp.add(mtsp,BorderLayout.CENTER);
		setContentPane(dp);			
		setIconImage(Imagem.ICON_BALLOON.getImage());				
		setSize(200, 550);
		CompCenterCords cords=GUIUtil.getCompCenterCords(200, 550);
		setLocation(cords.getX(), cords.getY());
		
		JMenu exitMenu=new JMenu("Arquivo");
		exitMenu.setFont(new Font("tahoma",5,12));
		JMenuItem exit=new JMenuItem("Sair");
		exit.setFont(new Font("tahoma",5,12));
		exit.addActionListener(new ExitActionListener(multicastSoc,group));
		exitMenu.add(exit);
		menuBar.add(exitMenu);
				
		setJMenuBar(menuBar);		
		setVisible(true);
	}
	
	public DefaultMutableTreeNode getTop() {
		return top;
	}

	public JTree getMainTree() {
		return mainTree;
	}
	
}